-- global functions

dofile("timer.lua")

-- clones only the first 'layer' of a table
function clone(t)            -- return a copy of the table t
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    new[i] = v
    i, v = next(t, i)        -- get next index
  end
  return new
end

-- clones a table completly (subtable inclusive)
function deepclone(t)
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    if type(v) == 'table' then -- if value is table then clone it
		v = deepclone(v)
    end
    new[i] = v
    i, v = next(t, i)        -- get next index
  end
  return new
end

function compare(object1x, object1y, object1wid, object1hig, object2x, object2y, object2wid, object2hig)
			if 	object1x + object1wid > object2x and 
				object1x < object2x + object2wid and 
				object1y + object1hig > object2y and 
				object1y < object2y + object2hig
			then return true
			end
end

function gameelementCollision(a,b) -- collision with offset
	if compare(a.center[1] + a.collisionCenter[1] - a.width *a.offsetWidth/2, a.center[2] + a.collisionCenter[2] -  a.height + a.offsetHeight/2, a.width *a.offsetWidth, a.height * a.offsetHeight, b.center[1] + b.collisionCenter[1] - b.width *b.offsetWidth/2, b.center[2] + b.collisionCenter[2] - a.height + b.offsetHeight/2, b.width *b.offsetWidth, b.height * b.offsetHeight) then return true end
end

function removeDestroydObjects(t)-- increase the performance
	
		for i,e in ipairs(t) do
			if 	e.currentState.name == "destroy" 
			then 
				table.remove (t,i)
				removeDestroydObjects(t)
				break
			end
		end

end
----------------------------------------------
--Verry ugly stuff remove as soon as possible--------------------------------------------
----------------------------------------------
function runStates() --this functions calls the state function of all physical objects. Im not happy to place this function here, but i didn't find a better place yet.
	for _,row in ipairs(PHYSICAL_OBJECT) do --no view implemented, paints now all objects
		for _,collumn in ipairs(row) do
			for _,element in ipairs(collumn) do
				element:runState()
			end
		end
	end
end

function checkStatus() --this functions calls the state function of all physical objects. Im not happy to place this function here, but i didn't find a better place yet.
	for _,row in ipairs(PHYSICAL_OBJECT) do --no view implemented, paints now all objects
		for _,collumn in ipairs(row) do
			for _,element in ipairs(collumn) do
				element:statusCheck()
			end
		end
	end
end

function cleanPO()
	for _,row in ipairs(PHYSICAL_OBJECT) do --no view implemented, paints now all objects
		for _,collumn in ipairs(row) do
			removeDestroydObjects(collumn)
		end
	end
end

--------------------------------------------------------------------------------------------
----------------------------------------------
----------------------------------------------


-- == wraping a coroutine
function sinus(steps)
steps = steps or 10
return coroutine.wrap(function () i = 0; while true do i = i + 2*math.pi/steps; if i == 2*math.pi then i = 0; end coroutine.yield(math.sin(i)); end  end)
end
--]]
 

moveRandomly = function(t, dirchange) 
				if math.random(1,dirchange or 30) == 1 then t.direction = math.random(1,8)
				end 
			end	
